import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.BackgroundCallback;
import org.apache.curator.framework.api.CuratorEvent;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.ZooDefs;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author create on 2021/7/9
 */
public class AsyncCuratorTest {
    public static void main(String[] args) throws Exception {
        String connIp = "127.0.0.1";
        ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3,Integer.MAX_VALUE);
        CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(connIp, retryPolicy);
        curatorFramework.start();

        /**
         * inBackground 方法是用来回调的， processResult 就是回调方法
         * 可以根据回调的响应码，去对Zk进行逻辑处理。
         */
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        curatorFramework.create()
                .creatingParentsIfNeeded()//递归创建,如果没有父节点,自动创建父节点
                .withMode(CreateMode.PERSISTENT)//节点类型,持久节点
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)//设置ACL,和原生API相同
                .inBackground(new BackgroundCallback() {
                    public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                        //回调之后的操作
                        byte[] data = client.getData().forPath("/node20");
                        System.out.println( "data..." +  new String(data));
                        System.out.println("===>响应码：" + event.getResultCode()+",type:" + event.getType());
                        System.out.println("===>Thread of processResult:"+Thread.currentThread().getName());
                        System.out.println("===>context参数回传：" + event.getContext());
                    }
                },"传给服务端的内容,异步会传回来", executorService)
                .forPath("/node20","123456".getBytes());
        Thread.sleep(3000);


        curatorFramework.create()
                .creatingParentsIfNeeded()//递归创建,如果没有父节点,自动创建父节点
                .withMode(CreateMode.PERSISTENT)//节点类型,持久节点
                .withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE)//设置ACL,和原生API相同
                .inBackground(new BackgroundCallback() {
                    public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                        System.out.println("===>响应码：" + event.getResultCode()+",type:" + event.getType());
                        System.out.println("===>Thread of processResult:"+Thread.currentThread().getId() + Thread.currentThread().getName());
                        System.out.println("===>context参数回传：" + event.getContext());
                    }
                },"传给服务端的内容,异步会传回来")
                .forPath("/node30","123456".getBytes());
        Thread.sleep(3000);
        executorService.shutdown();


    }
}
